package com.sskj.mine.presenter;

import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.PostRequest;
import com.sskj.mine.bean.UserAgreeMent;
import com.sskj.mine.http.HttpConfig;
import com.sskj.mine.ui.activity.RegisterActivity;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.http.JsonCallBack;


public class RegisterActivityPresenter extends BasePresenter<RegisterActivity> {


    /**
     * 手机号注册
     *
     * @param account        手机号
     * @param pswd           密码
     * @param code           验证码
     * @param invitationCode 邀请码
     * @return
     */
    public void register(String account, String pswd, String code, String invitationCode) {
        PostRequest<HttpData> httpDataPostRequest;
        if (CheckUtil.isEmail(account)) {//邮箱注册
            httpDataPostRequest = httpService.registerEmail(account, pswd, code, invitationCode);
        } else {//手机号码注册
            httpDataPostRequest = httpService.register(account, pswd, code, invitationCode);
        }
        httpDataPostRequest.execute(new JsonCallBack<HttpData>(this) {
            @Override
            public void onSuccess(Response<HttpData> response) {
                HttpData httpData = response.body();
                ToastUtil.showShort(httpData.getMsg());
                if (httpData.getStatus() == HttpConfig.OK) {
                    mView.registerSuccess();
                }
            }
        });
    }

    /**
     * 获取短信验证码---codeType 1代表注册  2代表重置密码
     *
     * @param mobile 手机号
     * @return
     */
    public void sendCode(String mobile) {
        httpService.sendCode(mobile, "1", "1", mobile)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        HttpData httpData = response.body();
                        ToastUtil.showShort(httpData.msg);
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.sendCodeOk();
                        } else {
                            mView.sendCodeError();
                        }
                    }

                    @Override
                    public void onError(Response<HttpData> response) {
                        mView.sendCodeError();
                        super.onError(response);
                    }
                });
    }
//
//    /**
//     * 邮箱注册
//     *
//     * @param email          邮箱号
//     * @param pswd           密码
//     * @param code           验证码
//     * @param invitationCode 邀请码
//     * @return
//     */
//    public void registerEmail(String email, String pswd, String code, String invitationCode) {
//        httpService.registerEmail(email, pswd, code, invitationCode)
//                .execute(new JsonCallBack<HttpData>() {
//                    @Override
//                    public void onSuccess(Response<HttpData> response) {
//                        HttpData httpData = response.body();
//                        ToastUtil.showShort(httpData.msg);
//                        if (httpData.getStatus() == HttpConfig.OK) {
//                            mView.sendCodeOk();
//                        } else {
//                            mView.sendCodeError();
//                        }
//                    }
//                });
//    }

    /**
     * 获取邮箱验证码---codeType 1代表注册  2代表重置密码
     *
     * @param email 邮箱号
     * @return
     */
    public void sendEmailCode(String email) {
        httpService.sendEmailCode(email)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        HttpData httpData = response.body();
                        ToastUtil.showShort(httpData.msg);
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.sendCodeOk();
                        } else {
                            mView.sendCodeError();
                        }
                    }

                    @Override
                    public void onError(Response<HttpData> response) {
                        mView.sendCodeError();
                        super.onError(response);
                    }
                });
    }


}
